﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using ibba.src.global;
using ibba.src.norisk.Contracts;
using ibba.src.norisk.BusinessLogic;

namespace ibba.src.norisk.ServiceImplementation
{
    [AspNetCompatibilityRequirements(RequirementsMode = System.ServiceModel.Activation.AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,
                     ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class ContratoVigencia : IContratoVigencia
    {
        private ContratoVigenciaBL _contratoVigenciaBL;

        public ContratoVigencia()
        {
            try
            {
                _contratoVigenciaBL = new ContratoVigenciaBL();
            }
            catch (Exception ex)
            {
                global.ErrorHandler.LogAndThrowFaultException(ex);
            }
        }

        public List<ibba.src.norisk.Contracts.ContratoVigencia> ObterContratoVigencia(string contrato, int idParametro)
        {
            List<Contracts.ContratoVigencia> retorno = null;

            try
            {
                retorno = TranslateContratoVigencia.Translate(_contratoVigenciaBL.ObterContratoVigencia(contrato, idParametro));
            }
            catch (Exception ex)
            {
                ErrorHandler.LogAndThrowFaultException(new FaultException(ex.ToString()));
            }

            return (retorno);
        }

        public int DesativarContratoVigencia(int? idVigencia, string cdContrato, string pCodUsuario, int idParametro)
        {
            int retorno = 0;

            try
            {
                retorno = _contratoVigenciaBL.DesativarContratoVigencia(idVigencia, cdContrato, pCodUsuario, idParametro);
            }
            catch (Exception ex)
            {
                ErrorHandler.LogAndThrowFaultException(new FaultException(ex.ToString()));
            }

            return retorno;
        }

        #region IContratoVigencia Members


        public int AtualizarContratoVigencia(List<ibba.src.norisk.Contracts.ContratoVigencia> pContrato, string pCodUsuario)
        {
            int retorno = 0;

            try
            {
                retorno = _contratoVigenciaBL.AtualizarContratoVigencia(TranslateContratoVigencia.Translate(pContrato), pCodUsuario);
            }
            catch (Exception ex)
            {
                ErrorHandler.LogAndThrowFaultException(new FaultException(ex.ToString()));
            }

            return retorno;
        }

        public List<String> ValidarCargaVigencias(List<ibba.src.norisk.Contracts.ContratoVigencia> pContrato)
        {
            List<String> list = new List<string>();

            try
            {
                list = _contratoVigenciaBL.ValidarCargaVigencias(TranslateContratoVigencia.Translate(pContrato));
            }
            catch (Exception ex)
            {
                global.ErrorHandler.LogAndThrowFaultException(ex);
            }

            return list;
        }


        public List<ibba.src.norisk.Contracts.ContratoVigencia> ListarContratoVigenciaLog(DateTime dtInicio, DateTime dtTermino)
        {
            List<Contracts.ContratoVigencia> retorno = null;

            try
            {
                retorno = TranslateContratoVigencia.Translate(_contratoVigenciaBL.ListarContratoVigenciaLog(dtInicio, dtTermino));
            }
            catch (Exception ex)
            {
                ErrorHandler.LogAndThrowFaultException(new FaultException(ex.ToString()));
            }

            return (retorno);
        }

        #endregion
    }
}
